#include "login.h"
#include "command.h"
#include <pwd.h>
#define MAXLEN 16
void getPassword(char *buffer, size_t maxLen) {
    struct termios oldt, newt;
    int i = 0;
    char ch;

    // 获取当前终端设置
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;

    // 关闭回显功能
    newt.c_lflag &= ~(ECHO);

    // 设置新终端属性
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    printf("请输入密码:");

    while (i < maxLen - 1) {
        ch = getchar();
        if (ch == '\n') break;

        buffer[i++] = ch;
        printf("*"); // 显示星号
        fflush(stdout);
    }
    buffer[i] = '\0';

    // 恢复终端设置
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    printf("\n");
}
int reg(int socketFd){
    while(1){
        int c;
        printf("请输入用户名:");
        char name[MAXLEN];
        scanf("%s",name);
        // 读取所有残留字符，直到缓冲区为空（遇到'\n'或EOF）
        while ((c = getchar()) != '\n' && c != EOF);
        char password[MAXLEN];
        getPassword(password, sizeof(password));
        int len=strlen(name);
        send(socketFd,&len,sizeof(int),0);
        send(socketFd,name,len,0);
        int saltLen;
        recv(socketFd,&saltLen,sizeof(int),MSG_WAITALL);
        if(saltLen==ERROR){
            printf("用户名已存在\n");
            continue;
        }
        char salt[21]={0};
        recv(socketFd,&salt,saltLen,MSG_WAITALL);
        char *pwd=NULL;
        pwd=crypt(password,salt);
        int pwdLen=strlen(pwd);
        send(socketFd,&pwdLen,sizeof(int),0);
        send(socketFd,pwd,pwdLen,0);
        int ret;
        recv(socketFd,&ret,sizeof(int),MSG_WAITALL);
        if(ret==REG){
            printf("注册成功\n");
            return 0;
        }
        else{
            return -1;
        }
    }
}
int login(int socketFd){
    int cnt=0;
    while(cnt<3){
        int c;
        printf("请输入用户名:");
        char name[MAXLEN];
        scanf("%s",name);
        // 读取所有残留字符，直到缓冲区为空（遇到'\n'或EOF）
        while ((c = getchar()) != '\n' && c != EOF);
        char password[MAXLEN];
        getPassword(password, sizeof(password));
        int len=strlen(name);
        send(socketFd,&len,sizeof(int),0);
        send(socketFd,name,len,0);
        int saltLen;
        recv(socketFd,&saltLen,sizeof(int),MSG_WAITALL);
        if(saltLen==ERROR){
            printf("用户名不存在\n");
            ++cnt;
            continue;
        }
        char salt[21]={0};
        recv(socketFd,&salt,saltLen,MSG_WAITALL);
        char *pwd=NULL;
        pwd=crypt(password,salt);
        int pwdLen=strlen(pwd);
        send(socketFd,&pwdLen,sizeof(int),0);
        send(socketFd,pwd,pwdLen,0);
        commandType ret;
        recv(socketFd,&ret,sizeof(int),MSG_WAITALL);
        if(ret==LOG){
            printf("登录成功\n");
            return 0;
        }
        else{
            printf("密码错误\n");
            ++cnt;
        }
    }
    printf("错误次数过多\n");
    return -1;
}
int usr(int socketFd) {
    int c;
    commandType type;
    int cnt=0;
    while(1){
        printf("注册0/登录1:");
        int cmd=0;
        scanf("%d",&cmd);
        while ((c = getchar()) != '\n' && c != EOF);
        if(cmd==0){
            // 注册
            type=REG;
            send(socketFd,&type,sizeof(int),0); 
            if(reg(socketFd)==0){
                return 0;
            }
        }
        else if(cmd==1){
            // 登录
            type=LOG;
            send(socketFd,&type,sizeof(int),0);
            if(login(socketFd)==0){
                return 0;
            }
            else{
                return 1;
            }
        }
        else{
            printf("无效命令,请重新输入!\n");
        }
    }
}

